/*
 * Copyright (c) 2006 Consolidated Court Automation Programs
 * All rights reserved.
 */
package example;

import gov.wicourts.wcca2.soap.tableexporter.ColumnSchema;
import gov.wicourts.wcca2.soap.tableexporter.ExportResult;
import gov.wicourts.wcca2.soap.tableexporter.Row;
import gov.wicourts.wcca2.soap.tableexporter.TableExporter;
import gov.wicourts.wcca2.soap.tableexporter.TableExporterServiceLocator;
import gov.wicourts.wcca2.soap.tableexporter.TableExporterServiceSoapBindingStub;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

/**
 * An example demonstrating basic use of the WCCA's table exporter service.
 */
public class AxisClient
{
	/** Set this to the username provided by CCAP */
	private static final String USER = "example";

	/** Set this to the password provided by CCAP */
	private static final String PASSWORD = "soapaccess/examplepass";

	/** The TableExporter SOAP service location */
	private static final String SERVICE_LOC =
		"http://wcca.wicourts.gov/services/TableExporterService";

	public static void main(String[] args)
	{
		try
		{
		    TableExporter exporter = getProxy();

		    ExportResult result = exporter.export("ChgDispoCode", null);
		    dumpResult(result);
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
		catch (ServiceException e)
		{
			e.printStackTrace();
		}
		catch (RemoteException e)
		{
			e.printStackTrace();
		}
	}
	
  /**
   * Dumps a retrieved table to <code>System.out</code>
   */
	private static void dumpResult(ExportResult result)
	{
	    ColumnSchema[] schema = result.getSchema();
	    System.out.println("Table has " + schema.length + " columns");
	    System.out.println("----------------");
	    for (ColumnSchema s : schema)
	    {
	        System.out.print(s.getName() + "(" + s.getType() + ") (" + s.isKey() + ")\t");
	    }
	    System.out.println();
	    System.out.println("----------------");
	    for (Row row : result.getRows())
	    {
	        for (String value : row.getItems())
	        {
	            System.out.print(value == null ? "" : value);
	            System.out.print('\t');
	        }
	        System.out.println();
	    }
	}

  /**
   * Returns a proxy object that can be used to query the remote service
   */
	private static TableExporter getProxy()
		throws MalformedURLException, ServiceException
	{
		TableExporterServiceLocator locator = new TableExporterServiceLocator();
		TableExporter result = locator.getTableExporterService(new URL(SERVICE_LOC));
		TableExporterServiceSoapBindingStub stub = (TableExporterServiceSoapBindingStub) result;
		stub.setUsername(USER);
		stub.setPassword(PASSWORD);
		return result;
	}

}
